package com.ziq.baselib.business.post

import android.app.Instrumentation
import android.content.Intent
import android.os.Build
import android.os.Bundle
import com.facebook.imagepipeline.request.ImageRequest
import com.ziq.base.base.BaseActivity
import com.ziq.base.extension.binding
import com.ziq.base.extension.click
import com.ziq.base.transition.ShareElementInfo
import com.ziq.base.transition.ShareElementInfoDataUpdate
import com.ziq.base.transition.TransitionHelper
import com.ziq.base.utils.ImageUtil
import com.ziq.baselib.business.post.bean.PostingDetail
import com.ziq.baselib.databinding.ActivityPostingDetailBinding
import dagger.hilt.android.AndroidEntryPoint


@AndroidEntryPoint
class PostingDetailActivity: BaseActivity() {

    private val binding : ActivityPostingDetailBinding by binding()

    //只能解决 A -> B -> C   B 到C 没有用共享元素 的话，B 回到 A 的共享元素动画能正常
    override fun onStop() {
        if (!isFinishing && Build.VERSION.SDK_INT == Build.VERSION_CODES.Q) {
            Instrumentation().callActivityOnSaveInstanceState(this, Bundle())
        }
        super.onStop()
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val item: PostingDetail = intent.getSerializableExtra("item") as PostingDetail
        binding.root
        var content = "无内容"
        if(item.post?.title?.isNotEmpty() == true){
            content = "${item.post?.title}"
        }else if(item.post?.content?.isNotEmpty() == true){
            content = "${item.post?.content}"
        }
        binding.title.text = content
        val imageInfo = item.post?.images?.get(0)
        if(imageInfo != null){
            ImageUtil.updateImageFromNetwork(binding.image, imageInfo.url, ImageRequest.CacheChoice.DEFAULT)
        }

        binding.image.click {
            val intent = Intent(this, PostingDetail2Activity::class.java)
            intent.putExtra("item", item)
            TransitionHelper.startActivity(this, intent)
        }

        TransitionHelper.setUpTransition(this,
            shareElementInfoList = {
                listOf(
                    ShareElementInfo<ShareElementInfoDataUpdate>("image", binding.image),
                    ShareElementInfo<ShareElementInfoDataUpdate>("title", binding.title),
                )
            }
        )
    }

}